//
//  DEMOSecondViewController.m
//  RESideMenuExample
//
//  Created by Roman Efimov on 10/10/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "HQSearchViewController.h"
#import "Masonry.h"
#import "HQZhou.h"
#import "HQViewController.h"
#import "UIView+Extension.h"
#import "HQDetailViewController.h"
#import "HQCollectionViewCell.h"
#import "HQNavigationController.h"
#import "UIView+Extension.h"
#import "UIButton+Extension.h"

//判断设备
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height == 568) ? YES : NO)
#define IS_IPhone6 (667 == [[UIScreen mainScreen] bounds].size.height ? YES : NO)
#define IS_IPhone6plus (736 == [[UIScreen mainScreen] bounds].size.height ? YES : NO)

@interface HQSearchViewController () <UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) NSArray *zhous;

@property (nonatomic, strong) NSMutableArray *showData;

@end

static NSString *searchID = @"searchID";

@implementation HQSearchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.title = @"搜索粥品";

    
    //添加左边按钮
    UIButton *leftButton = [UIButton buttonWithImage:@"nav_button_back" title:@"返回" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [leftButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

    
    //添加右边导航按钮
    UIButton *rightButton = [UIButton buttonWithImage:@"nav_button" title:@"主页" font:15 setColorRed:0.798 green:0.613 blue:0.392];
    [rightButton addTarget:self action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

    
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    search.placeholder = @"请输入粥品名称";
    search.delegate = self;
    self.searchBar = search;
    [self.view addSubview:search];
    
    [self prepareForUI];


}



//代理方法,searchBar
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//    NSLog(@"text改变了");
    if (searchText != nil && searchText.length > 0) {
        self.showData = [NSMutableArray array];
        for (HQZhou *zhou in self.zhous) {
            if ([zhou.name rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
                [self.showData addObject:zhou];
            }
        }
        [self.collectionView reloadData];
    }

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}




- (NSArray *)zhous{
    if (_zhous == nil) {
        _zhous = [HQZhou zhous];
    }
    return _zhous;
}



- (void)pushViewController:(id)sender
{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HQViewController *viewController = [sb instantiateViewControllerWithIdentifier:@"main"];
    [self.navigationController pushViewController:viewController animated:YES];

}

/*******************************分割线*******************************/

//准备UI
- (void)prepareForUI{
    
    //    NSLog(@"进入了准备UI");
    
    //设定参数
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(150, 170);
    layout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    
    if (IS_IPhone6) {
        layout.itemSize = CGSizeMake(170, 190);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    else if (IS_IPhone6plus) {
        layout.itemSize = CGSizeMake(190, 210);
        layout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    }

    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"HQCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:searchID];

    
    //指定代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    self.collectionView.y = 44;
    self.collectionView.height -= 128;
    
}

//数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.showData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:searchID forIndexPath:indexPath];
    HQZhou *zhou = self.showData[indexPath.item];
    cell.zhou = zhou;
    
    return cell;
}

//collection代理方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HQDetailViewController *detailVC = [[HQDetailViewController alloc] init];
    HQZhou *zhou = self.showData[indexPath.item];
    detailVC.zhou = zhou;
    detailVC.fromVC = @"searchVC";
    HQNavigationController *nav = [[HQNavigationController alloc] initWithRootViewController:detailVC];
    
    [self presentViewController:nav animated:true completion:nil];
    
}


@end
